package com.java.service.impl;

import com.java.mapper.SysAuthorityBeanMapper;
import com.java.mapper.SysUserBeanMapper;
import com.java.pojo.OneMenu;
import com.java.service.SysAuthorityService;
import com.java.service.SystemUserService;
import com.java.utils.MD5Tool;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @function:
 * @CreateTime: 2021/3/16 14:35
 */
@Service
public class SystemUserServiceImpl implements SystemUserService {

    @Autowired
    private SysUserBeanMapper sysUserBeanMapper;

    @Autowired
    private SysAuthorityBeanMapper sysAuthorityBeanMapper;

    /**
     * 添加用户信息
     * @param username
     * @param pwd
     * @param oneIds
     * @param twoIds
     * @return
     */
    @Transactional(readOnly = false)
    public boolean addSystemUser(String username, String pwd, String oneIds, String twoIds) {
        if(oneIds==null || "".equals(oneIds)) return false;
        if(twoIds==null || "".equals(twoIds)) return false;
        try {
            pwd = MD5Tool.md5(pwd);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Map map = new HashMap();
        map.put("username",username);
        map.put("pwd",pwd);
        int res = sysUserBeanMapper.addSystemUser(map);
        if(res<=0){ return false;}
        Long userId = (Long) map.get("userId");
        String[] one = oneIds.replaceAll("(.,)\\1+","$1").split(",");
        String[] two = twoIds.split(",");
        List list = new ArrayList();
        List<String> oneList = Arrays.asList(one);
        List<String> twoList = Arrays.asList(two);
        list.addAll(oneList);
        list.addAll(twoList);
        int i = sysAuthorityBeanMapper.insertSystemAuthortys(userId, list);
        System.out.println(i);
        if(i >= list.size()){
            return  true;
        }
        return false;
    }
    /**
     * 查询所有用户信息
     * @return
     */
    public List<Map<String, Object>> selectSystemUserAll(String pageNum,String pageSize) {
        return  sysUserBeanMapper.selectSystemUserAll();
    }

    public void updateSystemUserStatus(Long userId, String flag) {
         sysUserBeanMapper.updateSystemUserStatus(userId,flag);
    }
}
